<template>
  <div class="login">
    <div class="container">
      <a href="/#/index">
        <img src="/imgs/login-logo.png" alt />
      </a>
    </div>
    <div class="wrapper">
      <div class="container">
        <div class="login-form">
          <h3>
            <span class="checked">帐号登录</span>
            <span class="sep-line">|</span>
            <span>扫码登录</span>
          </h3>
          <div class="input">
            <input type="text" placeholder="请输入帐号" v-model="username" />
          </div>
          <div class="input">
            <input type="password" placeholder="请输入密码" v-model="password" />
          </div>
          <div class="btn-box">
            <a href="javascript:;" class="btn" @click="login">登录</a>
          </div>
          <div class="tips">
            <div class="sms">手机短信登录/注册</div>
            <div class="reg" @click="openRegister">
              立即注册
              <span>|</span>忘记密码？
            </div>
          </div>
        </div>
      </div>
    </div>
    <nav-footer></nav-footer>
    <!-- <Registerpop></Registerpop> -->

    <modal
      title="协议声明"
      sureText="同意"
      cancelText="不同意"
      btnType="3"
      modalType="large"
      v-bind:showModal="showModal"
      @submit="gotoRegister"
      @cancel="cancelModel"
    >
      <!-- 给插槽传值(新的插槽模板语法)： -->
      <template v-slot:body>
        <agreement></agreement>
      </template>
    </modal>
  </div>
</template>
<script>
import NavFooter from "./../components/NavFooter";
import Modal from "./../components/Modal";
import _agreement from "./../components/_agreement";
export default {
  name: "login",
  components: {
    NavFooter,
    agreement:_agreement,
    Modal
  },
  data() {
    return {
      username: "",
      password: "",
      userId: "",
      res: "",
      showModal: false,
      register_pop: false
    };
  },
  methods: {
    login() {
      //   let username=this.username;
      //   let password=this.password;
      let { username, password } = this;
      if(!this.username){
         this.$message.error("请输入用户名");
         return false
      }
      if(!this.password){
         this.$message.error("请输入密码");
         return false
      }      
      this.axios
        .post("/user/login", {
          username: username,
          password: password
        })
        .then(res => {
          this.res = res;
          //通过VueCookie插件设置cookie expiress:'Session':设置会话级别，关闭浏览器则清楚
          this.$cookie.set("userId", res.id, { expiress: "Session" });
          //保存用户名到vuex
          this.$store.dispatch("saveUserName", res.username);
          // 保存购物车数量vuex
          this.axios.get("/carts/products/sum").then(res => {
            // 截获购物车信息然后保存到VUEX
            this.$store.dispatch("saveCartCount", res);
          });
          //跳转到首页
          this.$router.push({
            name: "index",
            params: {
              from: "login"
            }
          });
        });
    },
    // 注册弹窗
    openRegister() {
      this.showModal = true;
    },
    gotoRegister() {
      this.$router.push('/register');
    },
    register() {
      this.axios
        .post("/user/register", {
          username: "ycd",
          password: "4336092196",
          email: "462085920@qq.com"
        })
        .then(() => {
          this.$message.success("注册成功");
        });
    },
    cancelModel() {
      this.showModal = false;
    }
  }
};
</script>
<style lang="less" scoped>
.login {
  & > .container {
    height: 113px;

    img {
      width: auto;
      height: 100%;
    }
  }
  .wrapper {
    background: url("/imgs/login-bg.jpg") no-repeat center;
    .container {
      height: 576px;
      position: relative;
      .login-form {
        box-sizing: border-box;
        padding-left: 31px;
        padding-right: 31px;
        width: 410px;
        height: 510px;
        background-color: #ffffff;
        position: absolute;
        bottom: 29px;
        right: 0;
        h3 {
          line-height: 23px;
          font-size: 24px;
          text-align: center;
          margin: 40px auto 49px;
          .checked {
            color: #ff6600;
          }
          .sep-line {
            margin: 0 32px;
          }
        }
        .input {
          display: inline-block;
          width: 348px;
          height: 50px;
          border: 1px solid #e5e5e5;
          margin-bottom: 20px;
          input {
            width: 100%;
            height: 100%;
            border: none;
            padding: 18px;
          }
        }
        .btn {
          width: 100%;
          line-height: 50px;
          margin-top: 10px;
          font-size: 16px;
          height: auto;
        }
        .tips {
          margin-top: 14px;
          display: flex;
          justify-content: space-between;
          font-size: 14px;
          cursor: pointer;
          .sms {
            color: #ff6600;
          }
          .reg {
            color: #999999;
            span {
              margin: 0 7px;
            }
          }
        }
      }
    }
  }
}
</style>